package com.jp.knowledge.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.activity.BaseFragment;
import com.jp.knowledge.activity.JpApplicationWebActivity;
import com.jp.knowledge.activity.JpMainActivity;
import com.jp.knowledge.activity.SearchSimpleActivity;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.view.TabHostNavView;
import com.jp.knowledge.view.TabHostPagerView;

import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * Created by jiayen on 16/9/28.
 */
public class AttentionFragment extends BaseFragment implements View.OnClickListener {

    private View  mView;
    @ViewInject(R.id.module_name)
    protected TextView topName;
    @ViewInject(R.id.module_desc)
    protected TextView  topDesc;
    @ViewInject(R.id.icon_serach)
    protected ImageView rightIcon;
    @ViewInject(R.id.them_tab_nav)
    private TabHostNavView tabHostNavView;
    @ViewInject(R.id.viewpager)
    private TabHostPagerView viewPage;

    private int size;
    private List<Fragment> fragments;
    private String[] itemNames = new String[]{"主题","公司","产品","人物"/*,"发布号"*/};
    private int[] colors=new int[]{R.color.them_color,R.color.company_color,R.color.product_color,R.color.figure_color/*,R.color.channel_color*/};
    private int[] imgs = new int[]{R.mipmap.leidazhutier,R.mipmap.leidagongsier,R.mipmap.leidachanpiner,R.mipmap.leidarenwuer/*,R.mipmap.leidafabuhaoer*/};

    private LocalBroadCast localBroad;
    private LocalBroadcastManager locaManager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.attention_fragment,null);
        x.view().inject(this,mView);
        initView();
        return mView;
    }

    /**
     * 设置关注
     */
    private void setAttention(){
        topName.setText("雷达");
        rightIcon.setImageResource(R.mipmap.guanzhutianjia);
        topDesc.setText("时刻为你关注一切");
    }

    private void initView() {
        setAttention();
        size = itemNames.length;
        tabHostNavView.setmNavNamesLs(Arrays.asList(itemNames),imgs,colors);
        if(fragments==null)
            fragments = new ArrayList<>();
        for (int i = 0; i < size; i++)
            fragments.add(new AttentionItemFragment().addFalg(i));

        viewPage.setOffscreenPageLimit(size);//全部加载出来
        viewPage.init(tabHostNavView,getActivity().getSupportFragmentManager(),fragments);

        rightIcon.setOnClickListener(this);
        registerBroadCast();//注册广播监听数据变化 针对h5的关注
    }

    /**
     * 注册监听广播
     */
    private void registerBroadCast() {
        locaManager = LocalBroadcastManager.getInstance(mContext);
        localBroad = new LocalBroadCast(new LocalBroadCast.BroadCastCallBack() {
            @Override
            public void done(Context context, Intent intent) {
                String action = intent.getAction();
                switch (action){
                    case LocalBroadCast.GET_ATTION_DATA_BROAD:
                        ((AttentionItemFragment) fragments.get(viewPage.getCurrentItem())).refresh(0, true);
                        break;
                    case LocalBroadCast.LOGOUT_SUCCESS_BROD:
                    case LocalBroadCast.GET_LOGIN_INFO:
                        for (Fragment fragment : fragments) {
                            AttentionItemFragment item = (AttentionItemFragment) fragment;
                            item.refresh(0, true);
                        }
                        break;
                }
            }
        });
        IntentFilter intent = new IntentFilter();
        intent.addAction(LocalBroadCast.GET_ATTION_DATA_BROAD);
        intent.addAction(LocalBroadCast.GET_LOGIN_INFO);
        intent.addAction(LocalBroadCast.LOGOUT_SUCCESS_BROD);
        locaManager.registerReceiver(localBroad,intent);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        locaManager.unregisterReceiver(localBroad);
    }

    @Override
    public void onClick(View view) {
        if(view.getId() == R.id.icon_serach)
            setWebLoadAttention();//去到web页面关注
    }

    /**
     * 去到web关注页面
     */
    private void setWebLoadAttention() {
        int postion = viewPage.getCurrentItem();
        String url = "";
        if(postion==0){
            url = "competeTheme/buildTheme";
        }else if(postion==1){//公司
//            url = "follow/companyList";
            SearchSimpleActivity.gotoSearch(mContext, 3, -1, null, true);
        }else if(postion==2){//产品产品
//            url = "follow/productList";
            SearchSimpleActivity.gotoSearch(mContext, 8, -1, null, true);
        }else if(postion==3){//follow/figureList
//            url = "follow/figureList";
            SearchSimpleActivity.gotoSearch(mContext, 4, -1, null, true);
        }else if(postion==4){//发布号follow/channelList
            url = "follow/channelList";
        }
        JpApplicationWebActivity.gotoWebActivity(mContext, url);
    }
}
